package com.mida.dms.knw.controller;

import com.mida.dms.knw.entity.KnwConceptualDomain;
import com.mida.dms.knw.entity.vo.KnwCodingConceptualDomainPageVo;
import com.mida.dms.knw.entity.vo.KnwConceptualDomainVo;
import com.mida.dms.knw.entity.vo.KnwConceptualVo;

import com.mida.dms.knw.service.KnwConceptualDomainService;
import com.mida.dms.knw.service.KnwConceptualService;
import com.mida.dms.knw.vo.KnwCodingConceptualDomainVo;
import com.mida.dms.knw.vo.KnwConceptualPageVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.dms.knw.vo.PullDownMenuVO;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.mida.dms.knw.Result.BaseResult.buildListSuccess;
import static com.mida.dms.knw.Result.BaseResult.buildListWarn;

/**
 * @author ：wang xiao le
 * @description：概念controller
 * @date ：2021/2/22 15:11
 */
@RestController
@RequestMapping("/knwConceptual")
@Api(tags = "概念")
@Slf4j
@CrossOrigin
public class KnwConceptualController  {
    @Autowired
    private KnwConceptualService knwConceptualService;

    @Autowired
    private KnwConceptualDomainService knwConceptualDomainService;

    /**
     * 1、新增概念基本信息
     *
     * @param knwConceptualVo
     * @return
     */
    @PostMapping("/addKnwConceptual")
    public ActionResult add(KnwConceptualVo knwConceptualVo) {
        return knwConceptualService.add(knwConceptualVo);
    }

    /**
     * /2、 更新概念
     *
     * @param knwConceptualVo
     * @return
     */
    @PostMapping("/updateKnwConceptual")
    public ActionResult update(KnwConceptualVo knwConceptualVo) {
        return knwConceptualService.update(knwConceptualVo);
    }


    /**
     * /3、 删除概念
     *
     * @param soid
     * @return
     */
    @GetMapping("/deleteKnwConceptual")
    public ActionResult delete(long soid) {
        return knwConceptualService.delete(soid);
    }

    /**
     * 4、获取概念
     *
     * @param soid
     * @return
     */
    @GetMapping("/getKnwConceptual")
    public ListResult<KnwConceptualVo> get(long soid) {
        return knwConceptualService.get(soid);
    }

    /**
     * 5、概念分页查询
     *
     * @param vo
     * @return
     */
    @PostMapping(value = "/findPage")
    public ListResult<PageResultVo<KnwConceptualVo>> findPage(KnwConceptualPageVo vo) {
        return knwConceptualService.findPage(vo);
    }

    /**
     * 6、查询全部概念
     *
     * @param
     * @return
     */
    @GetMapping(value = "/findAll")
    public ListResult<List<KnwConceptualVo>> findAll(@RequestParam(value = "searChData" ,required = false) String searChData,
                                                     @RequestParam(value = "conceptType",required = false) Long conceptType) {
        return knwConceptualService.findAll(searChData,conceptType);
    }
    /**
     *校验概念FSN重复性
     */
    @GetMapping(value = "/checkName")
    public ActionResult checkName(@RequestParam(value = "name",required = true) String name) {
        return knwConceptualService.checkName(name);
    }


//    /**
//     * 获取集值抽屉
//     */
//    @GetMapping("/drawer")
//    public ListResult<List<PullDownMenuVO>> drawer(String coding) {
//        ListResult result = new ListResult();
//        try {
//            List<PullDownMenuVO> data = detailCodingSystemService.drawer(coding);
//            buildListSuccess(result, data);
//        } catch (Exception e) {
//            log.error("获取值集错误", e);
//            buildListWarn(result, "获取值集错误");
//        }
//        return result;
//    }

    /**
     * 概念域绑定编码体系查询
     */
    @GetMapping(value = "/getKnwConceptualDomain")
    public ListResult<List<KnwConceptualDomainVo>> getKnwConceptualDomain(@RequestParam(value = "conceptualSoid",required = true) long conceptualSoid) {
        return knwConceptualDomainService.getKnwConceptualDomain(conceptualSoid);
    }

    /**
     * 概念域查询所有编码体系
     * @param domainSoid
     * @return
     */
    @GetMapping(value = "/getCodingDomainAll")
    public ListResult<List<KnwCodingConceptualDomainPageVo>> getCodingDomainAll(@RequestParam(value = "domainSoid",required = true) Long domainSoid) {
        return knwConceptualDomainService.getCodingDomainAll(domainSoid);
    }



    /**
     * 10、概念域分页查询
     *
     * @param domainSoid  概念域soid
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @return
     */
    @PostMapping(value = "/getCodingDomainPage")
    public ListResult<PageResultVo<KnwCodingConceptualDomainPageVo>> getCodingDomainPage(@RequestParam(value = "domainSoid") Long domainSoid,
                                                                                         @RequestParam(value = "currentPage") Long currentPage,
                                                                                         @RequestParam(value = "pageSize") Long pageSize) {
        return knwConceptualDomainService.findPage(domainSoid, currentPage, pageSize);
    }

    /**
     * 绑定编码体系业务处理
     * @param domainSoid
     * @param derailCodingSystemSoids
     * @return
     */
    @PostMapping(value = "/addDerailCodingSystem")
    public ActionResult addDerailCodingSystem(long domainSoid, String derailCodingSystemSoids) {
        return knwConceptualDomainService.add(domainSoid, derailCodingSystemSoids);
    }

    /**
     * 解绑业务处理
     * @param codingDomainSoid
     * @return
     */
    @PostMapping(value = "/deleteDerailCodingSystem")
    public ActionResult deleteDerailCodingSystem(long codingDomainSoid) {
        return knwConceptualDomainService.delete(codingDomainSoid);
    }

    /**
     * 更新首选标识业务处理
     * @param domainSoid
     * @param codingDomainSoid
     * @return
     */
    @PostMapping(value = "/updateIsPreferred")
    public ActionResult update(long codingDomainSoid , long domainSoid) {
        return knwConceptualDomainService.update(codingDomainSoid,domainSoid );
    }


    @GetMapping(value = "/getAllCodingDomain")
    public ListResult<List<KnwConceptualDomain>> getAllCodingDomain() {
        return knwConceptualDomainService.getAllCodingDomain();
    }

    /**
     * 更新概念状态
     * @param
     * @param status  状态soid
     * @return
     */
    @GetMapping("/updateStatus")
    public ActionResult updateStatus(@RequestParam Long status,@RequestParam Long conceptualSoid) {
        return knwConceptualService.updateStatus(status,conceptualSoid);
    }


    /**
     * 批量上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public ActionResult uploadFile(@RequestPart("file") MultipartFile file) {

        ActionResult result = new ActionResult();
        try {
            result= knwConceptualService.uploadFile(file);
        } catch (Exception e) {
            log.error("概念批量上传处理发生异常:"+ e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("概念批量上传处理失败");
            return result;
        }
        return result;
    }
}
